package com.fan.manydataresource.service.impl;

import com.fan.manydataresource.dao.cluster.CityDao;
import com.fan.manydataresource.dao.master.UserDao;
import com.fan.manydataresource.domain.City;
import com.fan.manydataresource.domain.User;
import com.fan.manydataresource.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionalEventListener;

/**
 * 用户业务实现层
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao; // 主数据源

    @Autowired
    private CityDao cityDao; // 从数据源

    @Override
    public User findByName(String userName) {
        User user = userDao.findByName(userName);
        City city = cityDao.findByName("温岭市");
        user.setCity(city);
        return user;
    }

    @Transactional(transactionManager = "masterTransactionManager")
    @Override
    public int insertTranscation() {
        User user = new User();
        user.setUserName("管家");
        user.setDescription("测试");
        userDao.addUser(user);
        addClub();
        int a = 1 / 0;
        return 0;
    }

    @Override

    public int addMaster() {
        User user = new User();
        user.setUserName("管家");
        user.setDescription("测试");
        return userDao.addUser(user);
    }

    @Override
    @Transactional(transactionManager = "clusterTransactionManager",propagation = Propagation.REQUIRES_NEW)
    public int addClub() {
        City city = new City();
        city.setCityName("合肥");
        city.setProvinceId(123L);
        city.setDescription("合肥市");
        cityDao.addCity(city);
        int a = 1 / 0;
        return 0;
    }
}
